﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public partial class operation
    {
        public static CommonClasses.Operacao[] DevolveOperacoesUtilizador(int UserID)
        {
            try
            {
                using (DBEntity entidade = new DBEntity())
                {
                    var carregamentos = (from ent in entidade.operation
                                         where ent.user.id == UserID
                                         && ent.document == null
                                         && ent.equipment == null
                                         select new CommonClasses.Operacao()
                                         {
                                             ID = ent.id,
                                             UserID = ent.user.id,
                                             Balance = ent.balance,
                                             Cost = ent.cost,
                                             DataOperacao = ent.date,
                                             Type = ent.type,
                                         }).ToList();

                    var documentos = (from ent in entidade.operation
                                      where ent.user.id == UserID
                                      && ent.document != null
                                      && ent.equipment == null
                                      select new CommonClasses.Operacao()
                                      {
                                          ID = ent.id,
                                          UserID = ent.user.id,
                                          Balance = ent.balance,
                                          Cost = ent.cost,
                                          DataOperacao = ent.date,
                                          Type = ent.type,
                                          Documento = new CommonClasses.Documento()
                                          {
                                              DocumentID = ent.document.id,
                                              Name = ent.document.name,
                                          },
                                      }).ToList();

                    var equipamentos = (from ent in entidade.operation
                                        where ent.user.id == UserID
                                        && ent.document == null
                                        && ent.equipment != null
                                        select new CommonClasses.Operacao()
                                        {
                                            ID = ent.id,
                                            UserID = ent.user.id,
                                            Balance = ent.balance,
                                            Cost = ent.cost,
                                            DataOperacao = ent.date,
                                            Type = ent.type,
                                            Equipamento = new CommonClasses.Equipamento()
                                            {
                                                ID = ent.equipment.id,
                                                Brand = ent.equipment.brand,
                                                Name = ent.equipment.name,
                                            },
                                        }).ToList();

                    List<CommonClasses.Operacao> todasOperacoes = new List<CommonClasses.Operacao>();
                    todasOperacoes.AddRange(carregamentos);
                    todasOperacoes.AddRange(equipamentos);
                    todasOperacoes.AddRange(documentos);

                    return todasOperacoes.ToArray();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
